function trim(str) {
    //删除左右两端的空格
    return str.replace(/(^\s*)|(\s*$)/g, "");
}
function ltrim(str) {
    //删除左边的空格
    return str.replace(/(^\s*)/g, "");
}
function rtrim(str) {
    //删除右边的空格
    return str.replace(/(\s*$)/g, "");
}
function checkNotNull(name, msg) {
    var obj = "txt" + name;
    var id = "lb" + name;
    if (trim(document.getElementById(obj).value).length < 1) {
        document.getElementById(id).innerHTML = msg;
        document.getElementById(id).style.color = "red";
        return false;
    }
    else {
        document.getElementById(id).innerHTML = "";
        return true;
    }

}
function checkusername(name) {
    var obj = "txt" + name;
    var username = document.getElementById(obj).value;
    var id = "lb" + name;
    if (trim(username).length < 1) {
        document.getElementById(id).innerHTML = "请填写用户名!";
        document.getElementById(id).style.color = "red";
        return false;
    }
    else {
        ///判断用户名
        var pattern = /^[A-Za-z0-9]+$/;
        var flag = pattern.test(username);
        if (flag) {
            document.getElementById(id).innerHTML = "";
        }
        else {
            document.getElementById(id).innerHTML = "仅使用数字和英文字母!";
            document.getElementById(id).style.color = "red";
            return false;
        }
    }
    return true;
}
function checkemail(name) {
    var obj = "txt" + name;
    var email = document.getElementById(obj).value;
    var id = "lb" + name;
    if (trim(email).length < 1) {
        document.getElementById(id).innerHTML = "请填写电子邮箱!";
        document.getElementById(id).style.color = "red";
        return false;
    }
    if (trim(email).length > 1) {
        ///判断邮箱
        var pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
        var flag = pattern.test(email);
        if (flag) {
            document.getElementById(id).innerHTML = "";
        }
        else {
            document.getElementById(id).innerHTML = "电子邮箱格式错误!";
            document.getElementById(id).style.color = "red";
            return false;
        }
    }
    return true;
}
function checkcellphone(name) {
    var obj = "txt" + name;
    var cellphone = document.getElementById(obj).value;
    var id = "lb" + name;
    if (trim(cellphone).length < 1) {

        document.getElementById(id).innerHTML = "请填写手机号码!";
        document.getElementById(id).style.color = "red";
        return false;
    }
    else {
        var pattern = /^\d{11}$/;
        var flag = pattern.test(cellphone);
        if (flag) {
            document.getElementById(id).innerHTML = "";
        }
        else {
            document.getElementById(id).innerHTML = "手机号码格式错误!";
            document.getElementById(id).style.color = "red";
            return false;
        }
    }
    return true;
}
function checkzipcode(name) {
    var obj = "txt" + name;
    var zipcode = document.getElementById(obj).value;
    var id = "lb" + name;
    if (trim(zipcode).length < 1) {

        document.getElementById(id).innerHTML = "请填写邮政编码!";
        document.getElementById(id).style.color = "red";
        return false;
    }
    else {
        var pattern = /^\d{6}$/;
        var flag = pattern.test(zipcode);
        if (flag) {
            document.getElementById(id).innerHTML = "";
        }
        else {
            document.getElementById(id).innerHTML = "邮政编码格式错误!";
            document.getElementById(id).style.color = "red";
            return false;
        }
    }
    return true;
}
function checkqualification(name) {
    var obj = "txt" + name;
    var qualification = document.getElementById(obj).value;
    var id = "lb" + name;
    if (trim(qualification).length < 1) {

        document.getElementById(id).innerHTML = "请填写资质证书编号!";
        document.getElementById(id).style.color = "red";
        return false;
    }
    else {
        document.getElementById(id).innerHTML = "";
        return true;
    }
    return true;
}
function checkunitcode(name) {
    var obj = "txt" + name;
    var unitcode = document.getElementById(obj).value;
    var id = "lb" + name;
    if (trim(unitcode).length < 1) {
        //document.getElementById(id).innerHTML = "请填写机构代码!";
        document.getElementById(id).style.color = "red";
        return false;
    }
    else {
        document.getElementById(id).innerHTML = "";
        return true;
    }
    return true;
}
function checkunitcodeupload(name) {
    var obj = "txt" + name;
    var unitcode = document.getElementById(obj).value;
    var id = "lb" + name;
    if (trim(unitcode).length < 1) {
        //document.getElementById(id).innerHTML = "请上传机构附件!";
        document.getElementById(id).style.color = "red";
        return false;
    }
    else {
        document.getElementById(id).innerHTML = "";
        return true;
    }
    return true;
}
function checkqualificationupload(name) {
    var obj = "txt" + name;
    var unitcode = document.getElementById(obj).value;
    var id = "lb" + name;
    if (trim(unitcode).length < 1) {
        document.getElementById(id).innerHTML = "请上传资质证书扫描件!";
        document.getElementById(id).style.color = "red";
        return false;
    }
    else {
        document.getElementById(id).innerHTML = "";
        return true;
    }
    return true;
}
function checklicenceupload(name) {
    var obj = "txt" + name;
    var unitcode = document.getElementById(obj).value;
    var id = "lb" + name;
    if (trim(unitcode).length < 1) {
        //document.getElementById(id).innerHTML = "请填上传营业执照附件!";
        document.getElementById(id).style.color = "red";
        return false;
    }
    else {
        document.getElementById(id).innerHTML = "";
        return true;
    }
    return true;
}
function checklicence(name) {
    var obj = "txt" + name;
    var unitcode = document.getElementById(obj).value;
    var id = "lb" + name;
    if (trim(unitcode).length < 1) {
        //document.getElementById(id).innerHTML = "请填写营业执照号!";
        document.getElementById(id).style.color = "red";
        return false;
    }
    else {
        document.getElementById(id).innerHTML = "";
        return true;
    }
    return true;
}

function checkrepeat(name, name2) {
    var obj = "txt" + name;
    var obj2 = "txt" + name2;
    var repeat = document.getElementById(obj).value;
    var source = document.getElementById(obj2).value;
    var id = "lb" + name;
    if (trim(repeat).length < 1) {

        document.getElementById(id).innerHTML = "请再次输入密码!";
        document.getElementById(id).style.color = "red";
        return false;
    }
    else {
        if (repeat == source) {
            document.getElementById(id).innerHTML = "";
        }
        else {
            document.getElementById(id).innerHTML = "输入密码不一致!";
            document.getElementById(id).style.color = "red";
            return false;
        }
    }
    return true;
}
function checkUserLogin() {
    //检查用户登录
    var username = document.getElementById("username");
    var userpass = document.getElementById("password");
    var i = true;
    while (i) {
        if (!checkNotNull(username, "请填写用户名")) break;
        if (!checkNotNull(userpass, "请填写密码")) break;
        i = false;
    }
    return !i;
}
function checkUserRegister() {
    var isvalid = true;
    var LICENSENO = true;
    var UNITCODE = true;
    if (!checkusername('username')) {
        isvalid = false;
    }
    if (!checkNotNull('password', '请输入密码!')) {
        isvalid = false;
    }
    if (document.getElementById("zzzs").style.display == "block" && !checkqualification('qualificationnum')) {
        isvalid = false;
    }
    if (!checkNotNull('unitname', '请填写单位名称!')) {
        isvalid = false;
    }

    if (!checkrepeat('surepassword', 'password')) {
        isvalid = false;
    }
    if (!checkNotNull('personname', '请输入真实姓名!')) {
        isvalid = false;
    }
    if (!checkNotNull('address', '请输入通信地址!')) {
        isvalid = false;
    }
    if (!checkcellphone('cellphone')) {
        isvalid = false;
    }
    if (document.getElementById("zzzs").style.display == "block" && !checkqualificationupload('qualification')) {
        isvalid = false;
    }
    if (!checkemail('email')) {
        isvalid = false;
    }
    if (!checkzipcode('zipcode')) {
        isvalid = false;
    }

    if (!checkunitcode('unitcode')) {
        UNITCODE = false;
    }
    if (UNITCODE && !checkunitcodeupload('unitcodeupload')) {
        UNITCODE = false;
    }
    if (!checklicence('licence')) {
        LICENSENO = false;
    }
    if (LICENSENO && !checklicenceupload('licenceupload')) {
        LICENSENO = false;
    }
    if (UNITCODE == false && LICENSENO == false) {
        document.getElementById('lbunitcode').innerHTML = "机构代码或者营业执照至少提交一种!";
        document.getElementById('lbunitcode').style.color = "red";
        isvalid = false;
    }
    else {
        document.getElementById('lbunitcode').innerHTML = "";
    }

    if (isvalid == false) {
        document.getElementById("uservalid").value = "0";
    }
    else {
        document.getElementById("uservalid").value = "1";
    }

    return isvalid;
}

function checkUnitRegister() {
    var isvalid = true;
    if (!checkNotNull('unitname', '请输入单位名称!')) {
        isvalid = false;
    }
    if (!checkNotNull('unitaddress', '请输入单位地址!')) {
        isvalid = false;
    }
    if (!checkunitcode('unitcode')) {
        isvalid = false;
    }
    if (!checkNotNull('repname', '请输入法人代表姓名!')) {
        isvalid = false;
    }
    //   if(!checkNotNull('certno','请输入资质证书编号!'))
    //   {
    //        isvalid=false;
    //   }
    if (!checkNotNull('contractman', '请输入联系人姓名!')) {
        isvalid = false;
    }
    if (!checkzipcode('zipcode')) {
        isvalid = false;
    }
    if (!checkcellphone('contractcellphone')) {
        isvalid = false;
    }
    if (!checkemail('contractemail')) {
        isvalid = false;
    }
    if (isvalid == false) {
        document.getElementById("unitvalid").value = "0";
    }
    else {
        document.getElementById("unitvalid").value = "1";
    }

    return isvalid;
}